package com.rongshu.view.home.presenter;


import android.content.Context;

import com.rongshu.R;
import com.rongshu.api.RetorfitService;
import com.rongshu.utils.Common;
import com.rongshu.utils.UserInfoUtils;
import com.rongshu.view.base.BaseActivity;
import com.rongshu.view.base.mvp.BasePresenter;
import com.rongshu.view.base.mvp.INetBean;
import com.rongshu.view.home.bean.BannerResponse;
import com.rongshu.view.home.bean.RecommendAuthorResponse;
import com.rongshu.view.home.bean.RecommendEditResponse;
import com.rongshu.view.home.bean.RecommendReaderResponse;
import com.rongshu.view.home.contract.DiscoveryView;
import com.widget.toast.ToastUtils;

import java.util.HashMap;
import java.util.Map;

import rx.Subscriber;
import rx.functions.Action0;

/**
 * created by zhengweis on 2018/11/1.
 * description：发现页面p
 */
public class DiscoveryPresenter extends BasePresenter<DiscoveryView> {
    BaseActivity activity;

    public DiscoveryPresenter(Context context) {
        super(context);
        activity = (BaseActivity) mContext;
    }

    public void getBannerImg() {
        Map<String, Object> map = new HashMap<>();
        map.put("user_id", UserInfoUtils.getUserId());
        RetorfitService.getBanner((int) map.get("user_id"), Common.encryptionSign(map))
                .compose(getView().<BannerResponse>bindToLife())
                .doOnSubscribe(new Action0() {
                    @Override
                    public void call() {

                    }
                })
                .subscribe(new Subscriber<BannerResponse>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(BannerResponse object) {
                        getView().bannerResponse(object);
                    }
                });
    }

    public void getRecommendAuthor(int pageIndex, int pageSize) {
        Map<String, Object> map = new HashMap<>();
        map.put("user_id", UserInfoUtils.getUserId());
        map.put("pageIndex", pageIndex);
        map.put("pageSize", pageSize);
        RetorfitService.getRecommendAuthor(UserInfoUtils.getUserId(), pageIndex, pageSize, Common.encryptionSign(map))
                .compose(getView().<RecommendAuthorResponse>bindToLife())
                .doOnSubscribe(new Action0() {
                    @Override
                    public void call() {

                    }
                })
                .subscribe(new Subscriber<RecommendAuthorResponse>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(RecommendAuthorResponse object) {
                        if (isViewAttached()) {
                            getView().recommendAuthorResponse(object);
                        } else {
                            ToastUtils.show("getView is null");
                        }
                    }
                });
    }

    public void getRecommendEdit() {
        Map<String, Object> map = new HashMap<>();
        map.put("user_id", UserInfoUtils.getUserId());
        RetorfitService.getRecommendEdit(UserInfoUtils.getUserId(), Common.encryptionSign(map))
                .compose(getView().<RecommendEditResponse>bindToLife())
                .doOnSubscribe(new Action0() {
                    @Override
                    public void call() {

                    }
                })
                .subscribe(new Subscriber<RecommendEditResponse>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(RecommendEditResponse object) {
                        getView().recommendEditResponse(object);
                    }
                });
    }

    public void getRecommedReader(int pageIndex, int pageSize) {
        Map<String, Object> map = new HashMap<>();
        map.put("user_id", UserInfoUtils.getUserId());
        map.put("pageIndex", pageIndex);
        map.put("pageSize", pageSize);
        RetorfitService.getRecommendReader(UserInfoUtils.getUserId(), pageIndex, pageSize, Common.encryptionSign(map))
                .compose(getView().<RecommendReaderResponse>bindToLife())
                .doOnSubscribe(new Action0() {
                    @Override
                    public void call() {

                    }
                }).subscribe(new Subscriber<RecommendReaderResponse>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
            }

            @Override
            public void onNext(RecommendReaderResponse object) {
                getView().recommendReaderResponse(object);
            }
        });
    }

    public void ConcernWith(int user_id_to) {
        Map<String, Object> map = new HashMap<>();
        map.put("user_id", UserInfoUtils.getUserId());
        map.put("contribute_id", 0);
        map.put("user_id_to", user_id_to);
        RetorfitService.ConcernWith(UserInfoUtils.getUserId(), 0, user_id_to, Common.encryptionSign(map))
                .compose(getView().<INetBean>bindToLife()).doOnSubscribe(new Action0() {
            @Override
            public void call() {

            }
        }).subscribe(new Subscriber<Object>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(Object object) {
                ToastUtils.show(mContext.getString(R.string.add_attention_success));
            }
        });
    }

    public void deleteRead(int contribute_id) {
        Map<String, Object> map = new HashMap<>();
        map.put("user_id", UserInfoUtils.getUserId());
        map.put("contribute_id", contribute_id);
        map.put("contribute_type", 1);
        RetorfitService.deleteRead(UserInfoUtils.getUserId(), contribute_id, 1, Common.encryptionSign(map))
                .compose(getView().<INetBean>bindToLife())
                .doOnSubscribe(new Action0() {
                    @Override
                    public void call() {

                    }
                })
                .subscribe(new Subscriber<INetBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(INetBean object) {
                    }
                });
    }

    /**
     * 最新活动
     public void getEvents() {
     Map<String, Object> map = new HashMap<>();
     map.put("user_id", UserInfoUtils.getUserId());
     map.put("pageIndex", 1);
     map.put("pageSize", 2);
     RetorfitService.getEvents(UserInfoUtils.getUserId(), 1, 2, Common.encryptionSign(map))
     .compose(getView().<EventListResponse>bindToLife())
     .doOnSubscribe(new Action0() {
    @Override public void call() {

    }
    })
     .subscribe(new Subscriber<EventListResponse>() {
    @Override public void onCompleted() {

    }

    @Override public void onError(Throwable e) {

    }

    @Override public void onNext(EventListResponse object) {
    if (object.getErrCode() == 0) {
    getView().eventsResponse(object);
    }
    }
    });
     }
     */
}
